package com.yfk.web.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

public class ResultDataUtil {
    /**
     * 接口请求成功
     * 
     * @param data
     * @return
     * @throws Exception
     */
    public static String success(Object data, String message) throws Exception {
        JSONObject res = new JSONObject();
        res.put("code", 200);
        if (message == null || "".equals(message)) {
            message = "操作成功";
        }
        res.put("message", message);
        res.put("data", JSON.parseArray(data.toString()));
        return res.toJSONString();
    }

    public static String error(String message) throws Exception {
        JSONObject res = new JSONObject();
        res.put("code", 500);
        if (StringUtils.isBlank(message)) {
            message = "操作异常,请联系管理员";
        }
        res.put("message", message);
        res.put("data", new JSONArray());
        return res.toJSONString();
    }
}